﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.Win32;

namespace SamRunInfo
{
    public class RunInfo
    {
        public delegate void MsgShowHandler(string msg);
        public event MsgShowHandler OnShowEvent;
        static RunInfo instance = null;
        Queue<string> m_Msga = new Queue<string>();
        Queue<string> m_Msgb = new Queue<string>();
        Queue<string> m_useMsg = null;
        Queue<string> m_saveMsg = null;
        Thread th = null;
        public string pathname = ".";
        string debugname = "Debug";
        private RunInfo()
        {
            m_useMsg = m_Msga;
            m_saveMsg = m_Msgb;
            try
            {
                pathname = Environment.CurrentDirectory;
                try
                {
                    RegistryKey regRead = Registry.LocalMachine.OpenSubKey("Software\\SamRunInfo", true);
                    if (regRead != null)
                    {
                        object obj = regRead.GetValue("pathName");
                        pathname = obj.ToString();
                    }
                }
                catch (Exception exx)
                {

                }
                string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                string[] sp = str.Split('\\');
                debugname = sp[sp.Length - 1];
            }
            catch (Exception ex)
            {
                //SamRunInfo.RunInfo.GetInstance().AddInfo(ex.ToString());
            }
        }
        public static RunInfo GetInstance()
        {
            if (instance == null)
            {
                instance = new RunInfo();
            }
            return instance;
        }
        public void Close()
        {
            if (th != null)
            {
                th.Abort();
            }
            Run();
            lock (m_useMsg)
            {
                m_saveMsg = m_useMsg;
                if (m_useMsg == m_Msga)
                {
                    m_useMsg = m_Msgb;
                }
                else
                {
                    m_useMsg = m_Msga;
                }
            }
            Run();
        }
        public void AddInfo(string msg)
        {
            if (m_useMsg.Count > 100)
            {
                lock (m_useMsg)
                {
                    m_saveMsg = m_useMsg;
                    if (m_useMsg == m_Msga)
                    {
                        m_useMsg = m_Msgb;
                    }
                    else
                    {
                        m_useMsg = m_Msga;
                    }
                }
                if (th == null)
                {
                    th = new Thread(new ThreadStart(Run));
                    th.Start();
                }
                else if (th.ThreadState != ThreadState.Running
                    && th.ThreadState != ThreadState.WaitSleepJoin)
                {
                    th = new Thread(new ThreadStart(Run));
                    th.Start();
                }
            }
            string nmsg = string.Format("{0}--{1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), msg);
            lock (m_useMsg)
            {
                m_useMsg.Enqueue(nmsg);
            }
            if (OnShowEvent != null)
            {
                OnShowEvent.Invoke(nmsg);
            }
        }
        public void Save()
        {
            lock (m_useMsg)
            {
                m_saveMsg = m_useMsg;
                if (m_useMsg == m_Msga)
                {
                    m_useMsg = m_Msgb;
                }
                else
                {
                    m_useMsg = m_Msga;
                }
            }
            if (th == null)
            {
                th = new Thread(new ThreadStart(Run));
                th.Start();
            }
            else if (th.ThreadState != ThreadState.Running
                && th.ThreadState != ThreadState.WaitSleepJoin)
            {
                th = new Thread(new ThreadStart(Run));
                th.Start();
            }
        }
        void Run()
        {
            bool bFull = false;
            lock (m_saveMsg)
            {
                StreamWriter sw = null;
                try
                {
                    sw = new StreamWriter(pathname + "\\" + debugname + ".txt", true);
                    while (m_saveMsg.Count > 0)
                    {
                        string s = m_saveMsg.Dequeue();
                        sw.WriteLine(s);
                    }
                    if (sw.BaseStream.Length > 67108864)
                    {
                        bFull = true;
                    }
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    if (sw != null)
                    {
                        sw.Close();
                        sw = null;
                    }
                }
                if (bFull)
                {
                    try
                    {
                        if (File.Exists(pathname + "\\" + debugname + "_old.txt"))
                        {
                            File.Delete(pathname + "\\" + debugname + "_old.txt");
                        }
                        if (File.Exists(pathname + "\\" + debugname + ".txt"))
                        {
                            File.Move(pathname + "\\" + debugname + ".txt", pathname + "\\" + debugname + "_old.txt");
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
        public int GetANum()
        {
            return m_Msga.Count;
        }
        public int GetBNum()
        {
            return m_Msgb.Count;
        }
    }
}
